/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset


* --------
* Table A7
* --------

* Globals
global dep1 "tax_rate risk mixed_dec differentiated valitrade_advice red_i"
global dep2 "tax_rate risk mixed_dec differentiated valitrade_advice red_i ln_ival_usd nup_gap_i_int"

* Matrix
mat tableA7 = J(24,4,.)
mat tableA7_T = J(1,4,.)

* Loop for estimations
** First column: Baseline
qui: reghdfe eis_f $dep1 countryorig* max_twodigit* if sample_reg==1 , absorb(ym_*) vce(cluster insp_f key_dec)
local f = 1
forvalues i = 1(1)6 {

	local x: word `i' of $dep1	

	mat tableA7[`f',1] = _b[`x']
	local f = `f' + 1
	mat tableA7[`f',1] = _se[`x']
	local f = `f' + 1
}
mat tableA7_T[1,1] = e(N)
mat tableA7[24,1] = e(r2)
** Second column: Adding inspector and broker FE
qui: reghdfe eis_f $dep1 countryorig* max_twodigit* ym_* if sample_reg==1 , absorb(key_dec ym_* insp_f) vce(cluster insp_f key_dec)
local f = 1
forvalues i = 1(1)6 {

	local x: word `i' of $dep1	

	mat tableA7[`f',2] = _b[`x']
	local f = `f' + 1
	mat tableA7[`f',2] = _se[`x']
	local f = `f' + 1
}
mat tableA7_T[1,2] = e(N)
mat tableA7[24,2] = e(r2)
** Third column: Adding unit price and initial value
qui: reghdfe eis_f $dep2 countryorig* max_twodigit* ym_* if sample_reg==1 , absorb(key_dec ym_* insp_f) vce(cluster insp_f key_dec)
local f = 1
forvalues i = 1(1)8 {

	local x: word `i' of $dep2	

	mat tableA7[`f',3] = _b[`x']
	local f = `f' + 1
	mat tableA7[`f',3] = _se[`x']
	local f = `f' + 1
}
mat tableA7_T[1,3] = e(N)
mat tableA7[24,3] = e(r2)
** Fourth column: Adding importer FE
qui: reghdfe eis_f $dep2 countryorig* max_twodigit* ym_* if sample_reg==1 , absorb(key_dec ym_* insp_f key_importer) vce(cluster insp_f key_dec)
local f = 1
forvalues i = 1(1)8 {

	local x: word `i' of $dep2	

	mat tableA7[`f',4] = _b[`x']
	local f = `f' + 1
	mat tableA7[`f',4] = _se[`x']
	local f = `f' + 1
}
mat tableA7_T[1,4] = e(N)
mat tableA7[24,4] = e(r2)


* Export
* ------
putexcel set "$tables\Table A7.xlsx", replace sheet(TableA7) // create a new excel spreadsheet

* Coefficients
putexcel B3 = matrix(tableA7), nformat(0.000) hcenter vcenter
putexcel B25 = matrix(tableA7_T), nformat(number_sep) hcenter vcenter

* Main titles
putexcel A1 = "Dependent variable:", left vcenter bold
putexcel (B1:E1), merge hcenter vcenter bold
putexcel B1 = "Excess interaction share"
local cells `" "B" "C" "D" "E" "'
forvalues i = 1(1)4 {

	local x: word `i' of `cells'

	putexcel `x'2 = "(`i')", hcenter vcenter
}

* Results
local j = 3
local vars `" "Tax rate" "Risk score" "Mixed shipment dummy" "Differentiated share" "Valuation advice dummy" "Red channel dummy" "Log initial value" "Log initial unit price (rel. to internal prices)" "'
forvalues i = 1/8 {

	local x: word `i' of `vars'

	putexcel A`j' = "`x'", left vcenter
	local j = `j'+2
}

* FE, Observations and R2
local j = 19
local fe `" "Month-year fixed effects" "Source country fixed effects" "HS2-product fixed effects" "Inspector fixed effects" "Broker fixed effects" "Importer fixed effects" "Observations" "R-squared" "'
forvalues i = 1(1)8 {

	local x: word `i' of `fe'

	putexcel A`j' = "`x'", left vcenter
	local j = `j'+1
}
local cells `" "B" "C" "D" "E" "'
forvalues i = 1(1)4 {

	local x: word `i' of `cells'

	forvalues j = 19(1)21 {
		putexcel `x'`j' = "Yes", hcenter vcenter
	}
}
forvalues j = 22(1)24 {

	putexcel B`j' = "No", hcenter vcenter
	
	if "`j'" == "24" {
		putexcel C`j' = "No", hcenter vcenter
		putexcel D`j' = "No", hcenter vcenter
		putexcel E`j' = "Yes", hcenter vcenter
	}
	
	else {
		putexcel C`j' = "Yes", hcenter vcenter
		putexcel D`j' = "Yes", hcenter vcenter
		putexcel E`j' = "Yes", hcenter vcenter
	}
}


* -------------------------------- ENDS HERE -------------------------------- *